package com.itc.dispatch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.common.core.domain.R;
import com.itc.common.security.utils.SecurityUtils;
import com.itc.dispatch.domain.FlightOperationMonitoring;
import com.itc.dispatch.domain.SysFlowType;
import com.itc.dispatch.domain.SysTagType;
import com.itc.dispatch.domain.dto.SysTagTypePageDto;
import com.itc.dispatch.service.FlightOperationMonitoringService;
import com.itc.dispatch.service.SysTagTypeService;
import com.itc.dispatch.mapper.SysTagTypeMapper;
import com.itc.system.api.RemoteSystemService;
import com.itc.system.api.domain.SysDept;
import com.itc.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
* @author 智少
* @description 针对表【sys_tag_type】的数据库操作Service实现
* @createDate 2024-08-22 17:01:01
*/
@Service
public class SysTagTypeServiceImpl extends ServiceImpl<SysTagTypeMapper, SysTagType>
    implements SysTagTypeService{

    @Autowired
    public FlightOperationMonitoringService flightService;

    @Resource
    public RemoteSystemService remoteSystemService;

    @Override
    public R<List<SysTagType>> getList(SysTagType sysTagType) {
        LambdaQueryWrapper<SysTagType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(sysTagType.getTagName() != null,SysTagType::getTagName,sysTagType.getTagName());
        queryWrapper.eq(SysTagType::getDelete,0);
        List<SysTagType> sysTagTypes = this.baseMapper.selectList(queryWrapper);
        return R.ok(sysTagTypes);
    }

    @Override
    public R<Page<SysTagType>> getPage(Page page, SysTagTypePageDto pageDto) {
        LambdaQueryWrapper<SysTagType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(pageDto.getTagName() != null,SysTagType::getTagName,pageDto.getTagName());
        queryWrapper.eq(SysTagType::getDelete,0);
        Page pageResult = this.baseMapper.selectPage(page, queryWrapper);
        return R.ok(pageResult);
    }

    @Override
    public R updateOne(SysTagType sysTagType) {
        SysTagType sysTagTypeOne = this.baseMapper.selectById(sysTagType.getId());
        if (!Objects.equals(sysTagTypeOne.getTagName(), sysTagType.getTagName()) && !Objects.equals(sysTagTypeOne.getTagType(), sysTagType.getTagType())){
            LambdaQueryWrapper<SysTagType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysTagType::getTagName,sysTagType.getTagName());
            queryWrapper.eq(SysTagType::getTagType,sysTagType.getTagType());
            queryWrapper.last("limit 1");
            Long aLong = this.baseMapper.selectCount(queryWrapper);
            if (aLong > 0){
                return R.fail("当前类型下已存在标签，更新失败");
            }
        }
        sysTagType.setFinalEnterTime(LocalDate.now().toString());
        this.baseMapper.updateById(sysTagType);
        return R.ok("更新成功");
    }

    @Override
    public R delete(Long id) {
        SysTagType sysTagType = this.baseMapper.selectById(id);
        if (sysTagType == null){
            return R.fail("数据有误");
        }
        LambdaQueryWrapper<FlightOperationMonitoring> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(FlightOperationMonitoring::getTags,sysTagType.getTagName());
        long aLong = flightService.count(lambdaQueryWrapper);
        if (aLong > 0){
            return R.fail("航班运行监控已关联，无法删除标签");
        }
        sysTagType.setDelete(1);
        this.baseMapper.updateById(sysTagType);
        return R.ok("删除成功");
    }

    @Override
    public R add(SysTagType sysTagType) {
        LambdaQueryWrapper<SysTagType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysTagType::getTagName,sysTagType.getTagName());
        queryWrapper.eq(SysTagType::getTagType,sysTagType.getTagType());
        queryWrapper.last("limit 1");
        Long aLong = this.baseMapper.selectCount(queryWrapper);
        if (aLong > 0 ){
            return R.fail("该类型下标签名称已经重复");
        }
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        //获取部门-席位,拼接字符串
        Long deptId = sysUser.getDeptId();
        SysDept dept= remoteSystemService.getDeptById(deptId);
        String deptChildIds = sysUser.getDeptChildId().toString();
        List<SysDept> deptChild = remoteSystemService.getDeptChildById(deptChildIds);
        String deptName=dept.getDeptName()+"-"+deptChild.get(0).getDeptName();
        //新增
        sysTagType.setDelete(0);
        sysTagType.setFinalEnterTime(LocalDate.now().toString());
        sysTagType.setEnterUserName(sysUser.getUserName());
        sysTagType.setDeptName(deptName);
        this.baseMapper.insert(sysTagType);
        return R.ok("新增成功");
    }
}




